package com.mhh.selary.backend.controller.project;

import com.mhh.sdk.beans.dto.BaseItemDTO;
import com.mhh.sdk.beans.dto.DropDownListDTO;
import com.mhh.selary.backend.api.project.ISelaryProjectService;
import com.mhh.selary.backend.dto.IdDTO;
import com.mhh.selary.backend.dto.project.ProjectGroupTreeDTO;
import com.mhh.selary.backend.dto.project.ProjectSaveDTO;
import com.mhh.selary.backend.dto.project.SubGroupSaveDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mhh
 * @date 2020-09-14 16:02:58
 */
@Slf4j
@RestController
@Api(tags = "项目管理")
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ISelaryProjectService selaryProjectService;

    @PostMapping("saveProject")
    @ApiOperation("保存项目")
    public IdDTO saveProject(@RequestBody ProjectSaveDTO dto) {
        IdDTO resultDTO = new IdDTO();
        resultDTO.setId(selaryProjectService.saveProject(dto));
        return resultDTO;
    }

    @PostMapping("delProject")
    @ApiOperation("删除项目")
    public void delProject(@RequestBody IdDTO dto) {
        selaryProjectService.delProject(dto.getId());
    }

    @PostMapping("saveGroup")
    @ApiOperation("保存分组")
    public IdDTO saveGroup(@RequestBody SubGroupSaveDTO dto) {
        IdDTO resultDTO = new IdDTO();
        resultDTO.setId(selaryProjectService.saveGroup(dto));
        return resultDTO;
    }

    @PostMapping("delGroup")
    @ApiOperation("删除分组")
    public void delGroup(@RequestBody IdDTO dto) {
        selaryProjectService.delGroup(dto.getId());
    }

    @PostMapping("groupTree")
    @ApiOperation("项目的分组信息")
    public ProjectGroupTreeDTO groupTree(@RequestBody IdDTO dto) {
        return selaryProjectService.groupTree(dto.getId());
    }

    @PostMapping("userProjectDropDown")
    @ApiOperation("用户可选择的项目选择下拉框")
    public DropDownListDTO userProjectDropDown() {
        List<BaseItemDTO> itemList = selaryProjectService.userProjectDropDown().stream()
                .map(projectDTO -> BaseItemDTO.builder()
                        .itemId(projectDTO.getId())
                        .itemName(projectDTO.getProjectName())
                        .itemCode(projectDTO.getProjectScope().toString())
                        .build())
                .collect(Collectors.toList());
        return DropDownListDTO.builder().dataList(itemList).build();
    }
}
